import os
import random
import math
import time

import cv2 as cv
import numpy as np
import creater.creatorMain

width = 2500
height = 2500


def createHole(_file_path, _res_file):
    # filepath = "/disk/data4T/yolov7_test/yolo_v7/realData/markyangben/circlemartrix/juzhenkong/7.5.bmp"
    # filepath = "/disk/data4T/yolov7_test/yolo_v7/realData/markyangben/ring/3.0/A/18.bmp"
    rand_img = creater.creatorMain.create_img_random(2500, 2500)
    # radius = random.randint(7, 15)

    radius = random.randint(80, 120)
    dx = (2 * radius) / height
    dy = (2 * radius) / width
    center0 = (random.randint(100, 200), random.randint(100, 200))
    color = random.randint(200, 235)
    # color = 235
    offset = 2 * radius + random.randint(50, 100)
    # offsetY = random.randint(50, 100)
    num_list = 6
    for i in range(1, num_list):
        for j in range(1, num_list):
            center = (center0[0] + i * offset, center0[1] + j * offset)
            centerx = center[0] / height
            centery = center[1] / width
            _res_file.write("4" + ' ')
            _res_file.write(str(centerx) + ' ')
            _res_file.write(str(centery) + ' ')
            _res_file.write(str(dx) + ' ')
            _res_file.write(str(dy) + ' ' + '\n')
            # xminp = (center[0] - radius)
            # yminp = (center[1] - radius)
            # xmaxp = (center[0] + radius)
            # ymaxp = (center[1] + radius)
            # cv.rectangle(rand_img, (xminp, yminp), (xmaxp, ymaxp), 0, 1)
            cv.circle(rand_img, center, radius, color, -1, cv.LINE_AA)
    ksize = (3, 3)
    sigma = 0
    blurred = cv.GaussianBlur(rand_img, ksize, sigma)
    cv.imwrite(_file_path, blurred)
    # cv.imshow("img", blurred)
    # cv.waitKey(0)


def rand_create_circle(dir_nums, img_nums, root_path):
    for i in range(1, dir_nums + 1):
        folder_path = os.path.join(root_path, str(i))
        res_path_d = os.path.join(folder_path, "res")
        print(i, " begin")
        if not os.path.exists(folder_path):
            os.makedirs(folder_path)
        if not os.path.exists(res_path_d):
            os.makedirs(res_path_d)
        for j in range(1, img_nums + 1):
            start_time = time.time()
            if not os.path.exists(folder_path):
                os.makedirs(folder_path)
            file_path = os.path.join(folder_path, "holes" + str(j) + '.bmp')
            res_path = os.path.join(res_path_d, "holes" + str(j) + ".txt")
            res_file = open(res_path, 'w')
            createHole(file_path, res_file)
            end_time = time.time()
            if j % 100 == 0:
                print(j, "-->finish", "time-->", end_time - start_time, '/pcs')

# /home/mikoptik/yolov7_test/yolo_v7/match_circle/


if __name__ == '__main__':
    root_dir = "/home/mikoptik/yolov7_test/yolo_v7/23_07_28run1"  # match_circle 23_07_28run
    dir_num = 1
    img_num = 5
    rand_create_circle(dir_num, img_num, root_dir)
